package study.springboot.server.support.tomcat;

import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;

/**
 * （1）Configurer
 * xxxConfigurer接口是一个配置器。实现该接口，编写具体的方法，来扩展Spring Boot中的配置。
 * （2）Customizer
 * xxxCustomizer接口是一个定制器。一般定制器只有一个方法，通过这个方法来修改Spring Boot的配置。
 */
@Slf4j
@Component
@ConditionalOnClass
public class _1_TomcatConfig implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        //
        TomcatConnectorCustomizer customizer = (connector) -> {
            Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
            log.info("");
            //线程
            log.info(">>>>>> max_thread= {}", protocol.getMaxThreads());
            log.info(">>>>>> min_spare_thread= {}", protocol.getMinSpareThreads());
            //
            log.info(">>>>>> max_connections= {}", protocol.getMaxConnections());
            log.info(">>>>>> accept_count= {}", protocol.getAcceptCount());
            log.info(">>>>>> connection_timeout= {}", protocol.getConnectionTimeout());

            //多少秒内没有请求则服务器自动断开 keepalive 连接
            log.info(">>>>>> keep_alive_timeout= {}", protocol.getKeepAliveTimeout());
            //当客户端发送超过 5 个请求则自动断开 keepalive 连接
            log.info(">>>>>> max_keep_alive_requests= {}", protocol.getMaxKeepAliveRequests());
            log.info(">>>>>> session_timeout= {}", protocol.getSessionTimeout());
            log.info("");
        };
        //
        factory.addConnectorCustomizers(customizer);
    }
}
